package com.zide.ams.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zide.ams.domain.po.AmsArticle;
import com.zide.ams.domain.po.AmsLike;
import com.zide.ams.domain.vo.result.AmsLikeResultVO;
import com.zide.ams.domain.vo.save.AmsLikeSaveVO;
import com.zide.ams.domain.vo.search.AmsLikeSearchVO;
import com.zide.ams.mapper.AmsArticleMapper;
import com.zide.ams.mapper.AmsLikeMapper;
import com.zide.ams.service.IAmsLikeService;
import com.zide.common.constant.HttpStatus;
import com.zide.common.core.domain.PageResult;
import cn.hutool.core.bean.BeanUtil;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 文章点赞Service业务层处理
 * 
 * @author zide
 * @date 2024-05-08
 */
@Slf4j
@Service
public class AmsLikeServiceImpl implements IAmsLikeService {
    @Resource
    private AmsLikeMapper amsLikeMapper;
    @Resource
    private AmsArticleMapper amsArticleMapper;

    /**
     * 查询文章点赞列表
     */
    @Override
    public PageResult<AmsLikeResultVO> searchAmsLikePageList(AmsLikeSearchVO searchVO) {
        PageResult<AmsLike> dbPageResult = amsLikeMapper.selectPage(searchVO);
        List<AmsLikeResultVO> resultList = BeanUtil.copyToList(dbPageResult.getList(),AmsLikeResultVO.class);
        PageResult<AmsLikeResultVO> result = new PageResult<>();
        result.setTotal(dbPageResult.getTotal());
        result.setList(resultList);
        return result;
    }

    /**
     * 新增文章点赞
     */
    @Override
    public int saveAmsLike(AmsLikeSaveVO saveVO) {
        long userId = SecurityUtils.getMemberUserId();
        if(userId == 0) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        AmsLike insertBean = BeanUtil.copyProperties(saveVO,AmsLike.class);
        insertBean.setCreateTime(LocalDateTime.now());
        insertBean.setUserId(userId);
        LambdaUpdateWrapper<AmsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("like_volume = like_volume + 1").eq(AmsArticle::getId,saveVO.getArticleId());
        amsArticleMapper.update(wrapper);

        return amsLikeMapper.insert(insertBean);
    }

    @Override
    public int cancelLike(AmsLikeSaveVO saveVO) {
        long userId = SecurityUtils.getMemberUserId();
        if(userId == 0) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        int row = amsLikeMapper.delete(new LambdaUpdateWrapper<AmsLike>()
                .eq(AmsLike::getArticleId, saveVO.getArticleId()).eq(AmsLike::getUserId, userId));
        if(row > 0) {
            LambdaUpdateWrapper<AmsArticle> wrapper = new LambdaUpdateWrapper<>();
            wrapper.setSql("like_volume = like_volume - 1").eq(AmsArticle::getId,saveVO.getArticleId());
            amsArticleMapper.update(wrapper);
        }
        return row;
    }
}
